package com.dongdongshop.controller;

import com.dongdongshop.data.PageResult;
import com.dongdongshop.data.PageUtil;
import com.dongdongshop.data.Result;
import com.dongdongshop.domain.vo.BrandVO;
import com.dongdongshop.domain.vo.SpecificationOptionVO;
import com.dongdongshop.domain.vo.SpecificationVO;
import com.dongdongshop.service.SpecificationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("spec")
@CrossOrigin
@Api("运营商后台管理——规格管理")
@RefreshScope   //自动监听配置文件
@Slf4j
public class SpecificationController {

    @Autowired
    private SpecificationService specificationService;

    //分页模糊查询
    @PostMapping("specList")
    @ApiOperation(value = "分页模糊查询")
    @ApiImplicitParam(name = "parms", value = "分页工具类", required = true, paramType = "body")
    public Result specList(@RequestBody PageResult<SpecificationVO> parms){
        PageUtil<SpecificationVO> page = specificationService.specList(parms);
        return Result.OK().setData(page);
    }

    //增加
    @PostMapping("add")
    @ApiOperation(value = "规格管理增加")
    @ApiImplicitParam(name = "vo", value = "规格表", required = true, paramType = "body")
    public Result addSpec(@RequestBody SpecificationVO vo){
        specificationService.addSpec(vo);
        return Result.OK();
    }

    //根据id查询/回显
    @PostMapping("selectById")
    @ApiOperation(value = "根据id查询")
    @ApiImplicitParam(name = "id", value = "主键id", required = true, paramType = "query")
    public Result selectById(@RequestParam Long id){
        SpecificationVO vo = specificationService.selectById(id);
        return Result.OK().setData(vo);
    }

    //修改
    @PutMapping("update")
    @ApiOperation(value = "规格管理修改")
    @ApiImplicitParam(name = "vo", value = "规格表", required = true, paramType = "body")
    public Result update(@RequestBody SpecificationVO vo){
        specificationService.updateSpec(vo);
        return Result.OK();
    }

    //根据id删除
    @DeleteMapping("deleteById")
    @ApiOperation(value = "根据id删除")
    @ApiImplicitParam(name = "id", value = "主键id", required = true, paramType = "query")
    public Result deleteById(@RequestParam Long id){
        specificationService.deleteById(id);
        return Result.OK();
    }

    //批量删除
    @DeleteMapping("delete")
    @ApiOperation(value = "批量删除")
    @ApiImplicitParam(name = "ids", value = "主键id集合", required = true, paramType = "body")
    public Result delete(@RequestBody List<Long> ids){
        specificationService.delete(ids);
        return Result.OK();
    }

    //根据id删除
    @DeleteMapping("delThis")
    @ApiOperation(value = "根据id删除规格选项")
    @ApiImplicitParam(name = "id", value = "主键id", required = true, paramType = "query")
    public Result delThis(@RequestParam Long id) {
        specificationService.delThis(id);
        return Result.OK();
    }

    //查询全部
    @PostMapping("getAllSpec")
    @ApiOperation(value = "查询全部")
    public Result getAllSpec(){
        List<SpecificationVO> voList = specificationService.getAllSpec();
        return Result.OK().setData(voList);
    }

}
